package cn.itcast.bos.service.take_delivery.impl;

import cn.itcast.bos.dao.take_delivery.WayBillDao;
import cn.itcast.bos.domain.take_delivery.WayBill;
import cn.itcast.bos.service.take_delivery.WayBillService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class WayBillServiceImpl implements WayBillService {
    @Autowired
    private WayBillDao wayBillDao;
    @Override
    public void save(WayBill wayBill) {
        WayBill persistWayBill = wayBillDao.findByWayBillNum(wayBill.getWayBillNum());
        if (persistWayBill == null) {
            wayBillDao.save(wayBill);
        }else {
            Integer id = persistWayBill.getId();
            BeanUtils.copyProperties(persistWayBill,wayBill);
            persistWayBill.setId(id);
        }
        wayBillDao.save(wayBill);
    }

    @Override
    public Page<WayBill> pageQuery(Pageable pageable) {
        return wayBillDao.findAll(pageable);
    }

    @Override
    public WayBill findByWayBillNum(String wayBillNum) {
        return wayBillDao.findByWayBillNum(wayBillNum);
    }
}
